package com.exmple.myapplication

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.DocumentsContract
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.max

class Demo : AppCompatActivity() {

    private var btnSelectDir: Button? = null
    private var btnSelectFile: Button? = null
    private var btnSave: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_second)
        btnSelectDir = findViewById(R.id.button_second)
        btnSelectFile = findViewById(R.id.button_second_4)
        btnSave = findViewById(R.id.button_second_2)
        registerForActivityResult<Uri, Uri>(ActivityResultContracts.OpenDocumentTree()) { result: Uri? ->
            if (result == null) {
                return@registerForActivityResult
            }
            val uri = DocumentsContract.buildDocumentUriUsingTree(
                    result,
                    DocumentsContract.getTreeDocumentId(result)
            )
            try {
                FirstFragment.selectPath = ReaderUtils.getImageAbsolutePath(this, uri)
            } catch (exception: ArrayIndexOutOfBoundsException) {
                FirstFragment.selectPath = Environment.getExternalStorageDirectory().absolutePath
            }

        }
        btnSelectDir?.setOnClickListener {

        }
        registerForActivityResult(ActivityResultContracts.GetMultipleContents()) {
            if (it == null
            )
                return@registerForActivityResult
            it.forEachIndexed { index, uri -> println("${index}是${uri}") }
            mergeBitmap(it)
        }.launch("*/*")
    }

    private fun mergeBitmap(it: List<@JvmSuppressWildcards Uri>) {
        val bitmaps = ArrayList<Bitmap>()

        var totalH = 0
        var maxW = 0
        it.forEach {
            val imageAbsolutePath = ReaderUtils.getImageAbsolutePath(this, it)
            val bitmap = BitmapFactory.decodeFile(imageAbsolutePath)
            bitmaps += bitmap
        }

        bitmaps.forEach { bitmap ->
            maxW = max(maxW, bitmap.width)
        }

        bitmaps.forEachIndexed { index, bitmap: Bitmap ->
            val matrix = Matrix()
            val fl = (maxW * 1.0f) / (bitmap.width * 1.0f)
            matrix.setScale(fl, fl)
            val bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, false)
            bitmaps[index] = bitmap2
            totalH += bitmap2.height
        }
        val createBitmap = Bitmap.createBitmap(maxW, totalH, bitmaps[0].config)
        val canvas = Canvas(createBitmap)
        var top = 0f
        bitmaps.forEach {
            canvas.drawBitmap(it, 0f, top, null)
            top+=it.height
        }

    }
}